package com.sxt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.sxt.listener.Receiver;
import com.sxt.listener.RedisReceiver;
import com.sxt.listener.UserInfoReceiver;

/**
 * redis配置类
 * 
 * @program: springboot
 * @Date: 2020/1/6 15:20
 * @Author: luolin
 * @Description:
 */
@Configuration
public class RedisConfig {
	
	
	/**
	 * retemplate相关配置
	 * 
	 * @param factory
	 * @return
	 */
	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, Object> template = new RedisTemplate<>();
		// 配置连接工厂
		template.setConnectionFactory(factory);
		// 自定义序列化方式
		template.setDefaultSerializer(new FastJsonRedisSerializer<>(Object.class));
		return template;
	}

	/**
	 * 对hash类型的数据操作
	 *
	 * @param redisTemplate
	 * @return
	 */
	@Bean
	public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {

		return redisTemplate.opsForHash();
	}

	/**
	 * 对redis字符串类型数据操作
	 *
	 * @param redisTemplate
	 * @return
	 */
	@Bean
	public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForValue();
	}

	/**
	 * 对链表类型的数据操作
	 *
	 * @param redisTemplate
	 * @return
	 */
	@Bean
	public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForList();
	}

	/**
	 * 对无序集合类型的数据操作
	 *
	 * @param redisTemplate
	 * @return
	 */
	@Bean
	public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForSet();
	}

	/**
	 * 对有序集合类型的数据操作
	 *
	 * @param redisTemplate
	 * @return
	 */
	@Bean
	public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForZSet();
	}
//==================================== Redis发布订阅分割线  ====================================//
	/**
	 * 	定义消费者
	 * redis消息监听器容器 可以添加多个监听不同话题的redis监听器，
	 * 	只需要把消息监听器和相应的消息订阅处理器绑定，该消息监听器
	 *	 通过反射技术调用消息订阅处理器的相关方法进行一些业务处理
	 * public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,MessageListenerAdapter listenerAdapter)
	 * @param connectionFactory
	 * @param listenerAdapter
	 * @return
	 */
	// MessageListenerAdapter 表示监听频道的不同订阅者
	@Bean
	public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(connectionFactory);
		//订阅多个频道
		//container.addMessageListener(listenerAdapter(), topic());
		container.addMessageListener(listenerAdapter(), new PatternTopic("topic1"));//配置要订阅的订阅项
		container.addMessageListener(listenerAdapter2(), new PatternTopic("topic2"));
		return container;
	}
	/**
     * 	消息监听器适配器，绑定消息处理器，利用反射技术调用消息处理器的业务方法
     * @param receiver
     * @return
     */
	@Bean
	public MessageListenerAdapter listenerAdapter() {
		/**
		 * 	通过反射机制
		 * 1.  参数1表示监听类
		 * 2. 参数2表示方法名
		 */
		return new MessageListenerAdapter(new RedisReceiver(), "receiveMessage");
	}

	@Bean
	public MessageListenerAdapter listenerAdapter2() {
		/**
		 * 	通过反射机制
		 * 1.  参数1表示监听类
		 * 2. 参数2表示方法名
		 */
		return new MessageListenerAdapter(new UserInfoReceiver(), "receiveMessage");
	}
	
	@Bean
	public ChannelTopic topic() {
		return new ChannelTopic("messageQueue");
	}
}
